package com.cskaoyan.demo;

import com.cskaoyan.Account;
import com.cskaoyan.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import javax.sql.DataSource;
import java.sql.SQLException;

public class QueryRunnerDemo2 {

    public static void main(String[] args) throws SQLException {

        // 获取数据库连接池
        DataSource dataSource = DruidUtils.getDataSource();

        // 构建一个QueryRunner对象
        QueryRunner queryRunner = new QueryRunner(dataSource);

        // 执行SQL语句
        // 需要传入字节码对象，后续通过反射根据字节码对象创建实例，然后再去通过反射或者Bean里面成员变量的setter方法，然后去赋值
        Account account = queryRunner.query("select * from account where id = ?",new BeanHandler<>(Account.class),2);

        System.out.println(account);

    }
}
